package com.cloudcc.mobile.presenter;

import java.util.HashMap;
import java.util.Map;

import android.text.TextUtils;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.net.RequestListener;
import com.cloudcc.mobile.dao.IBeauEngine;
import com.cloudcc.mobile.dao.impl.BeanEngineImpl;
import com.cloudcc.mobile.dao.impl.EntityEngineImpl;
import com.cloudcc.mobile.event.BeauEventList;
import com.cloudcc.mobile.event.BeauEventList.BeauInfoEvent;
import com.cloudcc.mobile.event.BeauEventList.BeauListEvent;
import com.cloudcc.mobile.event.BeauEventList.BeauListFilterEvent;
import com.cloudcc.mobile.event.BeauEventList.DynamicListEvent;
import com.cloudcc.mobile.event.BeauEventList.RelateionFollowListEvent;
import com.google.gson.JsonObject;

public class BeauPresenter extends BasePresenter {

	private IBeauEngine mEngine;

	public BeauPresenter() {
		super();
		mEngine = new BeanEngineImpl();
	}

	/**
	 * 获取对象列表
	 * 
	 * @param viewId
	 *            视图id
	 * @param pageNum
	 *            页数
	 * @param sortBy
	 *            排序
	 * @param sortModel
	 *            排序规则 正序 是 true
	 * @param keyword
	 *            检索关键字
	 */
	public void getBeanList(FilterCondition condition) {
		BeauListEvent event = new BeauEventList.BeauListEvent();

		// 检测
		if (TextUtils.isEmpty(condition.viewId)){//视图
			event.setOk(false);
			event.setMessage("亲,请选择视图~~");
			EventEngine.post(event);
			return;
		}
		
		//筛选条件
		StringBuilder sb =new StringBuilder();
		if (condition.filterFileds != null
				&& condition.filterFileds.size() > 0) {
			int index =0;
			//sb.append("\"");
			for (Map.Entry<String, String> entry : condition.filterFileds.entrySet()) {
				if (index!=0) {
					sb.append(" and ");
				}
				sb.append(entry.getKey());
				sb.append("=");
				sb.append("'"+entry.getValue()+"'");
				index++;
			}
			//sb.append("\"");
		}
		mEngine.serachBeauList(condition.viewId, condition.pageNum, condition.sortFiled, condition.sorModel, condition.keyword,sb.toString(), event);
	}

	public void getBeanFilter(String objId) {
		BeauListFilterEvent event = new BeauListFilterEvent();
		mEngine.getFilter(objId, event);
	}

	public void getBeauInfo(String id,String objApiName,String objArrays) {
		BeauInfoEvent event = new BeauInfoEvent();
		mEngine.getBeauInfo(id, objApiName, objArrays, event);
	}

	public void getBeauRelateDynamic(String id, int pageNum) {
		DynamicListEvent event = new DynamicListEvent();
		mEngine.getBeauRelatDynamic("", event);
	}
	
	/** 获取相关列表的 中 字段跟踪记录的
	 * @param layoutId
	 * @param recoredId
	 * @param relatedlistId
	 */
	public void getBeauRelationFollows(String layoutId,String recoredId,String relatedlistId){
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("recordId", recoredId);
		jsonObject.addProperty("relatedlistId", relatedlistId);
		jsonObject.addProperty("layoutId", layoutId);
		RelateionFollowListEvent event =new RelateionFollowListEvent();
		mEngine.getBeauRelatFollows(jsonObject, event);
	}
	
	
	/** 删除对象
	 * @param id
	 * @param objectApiName
	 * @param requestListener
	 */
	public void deleteEntity(String id,String objectApiName,RequestListener requestListener){
		new EntityEngineImpl().deleteEntity(id, objectApiName, requestListener);
	}
	

	/**
	 * 查询条件
	 */
	public static class FilterCondition {
		public String sortFiled = "";
		public boolean sorModel = false;
		public String viewId;
		public int pageNum;
		public String keyword;
		public Map<String,String> filterFileds =new HashMap<String, String>();

	}
	
	
	

}
